using System;
using System.Collections.Generic;
using CodePlex.TfsLibrary.ObjectModel;
using CodePlex.TfsLibrary.RepositoryWebSvc;
using Xunit;

namespace CodePlex.TfsLibrary.ClientEngine
{
    public class TfsEngineListTest
    {
        [Fact]
        public void CallsQueryItemsOnSourceControlService()
        {
            List<SourceItem> results = new List<SourceItem>();
            TestableTfsEngine engine = TestableTfsEngine.Create();

            engine.List("http://tfsUrl", "$/Foo", true, VersionSpec.Latest,
                        delegate(SourceItem item,
                                 SourceItemResult result)
                        {
                            results.Add(item);
                        });

            Assert.Equal("http://tfsUrl", engine.SourceControlService.QueryItems_TfsUrl);
            Assert.Equal("$/Foo", engine.SourceControlService.QueryItems_ServerPath);
            Assert.Equal(RecursionType.Full, engine.SourceControlService.QueryItems_Recursion);
            Assert.Equal<VersionSpec>(VersionSpec.Latest, engine.SourceControlService.QueryItems_VersionSpec);
            Assert.Equal(DeletedState.NonDeleted, engine.SourceControlService.QueryItems_DeletedState);
            Assert.Equal(ItemType.Any, engine.SourceControlService.QueryItems_ItemType);

            foreach (SourceItem item in results)
                Assert.Contains(item, engine.SourceControlService.QueryItems__Result);
        }

        [Fact]
        public void NonRecursive()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();

            engine.List("http://tfsUrl", "$/Foo", false, VersionSpec.Latest, delegate {});

            Assert.Equal(RecursionType.OneLevel, engine.SourceControlService.QueryItems_Recursion);
        }

        [Fact]
        public void NullDelegateThrows()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();

            Assert.Throws<ArgumentNullException>(delegate
                                                 {
                                                     engine.List("http://tfsUrl", "$/Foo", true, VersionSpec.Latest, null);
                                                 });
        }

        [Fact]
        public void NullServerPathThrows()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();

            Assert.Throws<ArgumentNullException>(delegate
                                                 {
                                                     engine.List("http://tfsUrl", null, true, VersionSpec.Latest, delegate {});
                                                 });
        }

        [Fact]
        public void NullTfsUrlThrows()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();

            Assert.Throws<ArgumentNullException>(delegate
                                                 {
                                                     engine.List(null, "$/Foo", true, VersionSpec.Latest, delegate {});
                                                 });
        }

        [Fact]
        public void NullVersionSpecThrows()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();

            Assert.Throws<ArgumentNullException>(delegate
                                                 {
                                                     engine.List("http://tfsUrl", "$/Foo", true, null, delegate {});
                                                 });
        }
    }
}